// var http = require('http');
// var net = require('net');
// var url = require('url');

import http from 'http'
import net from 'net'
import { URL } from 'url'

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function connect(cReq: any, cSock: any) {
  const u = new URL('http://' + cReq.url)
  console.log('AT-[ u &&&&&********** ]', u.host)

  const pSock = net
    .connect(Number(u.port), u.hostname, function () {
      cSock.write('HTTP/1.1 200 Connection Established\r\n\r\n')
      pSock.pipe(cSock)
    })
    .on('error', function (error) {
      console.log('AT-[ connect error &&&&&********** ]', error)
      cSock.end()
    })

  cSock.pipe(pSock)
}

http
  .createServer()
  .on('connect', connect)
  .on('error', (error) => {
    console.log('AT-[ http error &&&&&********** ]', error)
  })
  .listen(7777, '0.0.0.0', () => {
    console.log('https 代理服务器已经启动', 7777)
  })
